(function(angular) {
	'use strict';
	angular.module('fs.waf').controller('cda.ratingType.updateVerifyController', editController);
	editController.$inject = [ '$scope', 'Constants', 'modalService', 'gridService', 'layoutConfig', 'validatorConfig', 'authService',
			'ratingTypeService' ];
	function editController($scope, Constants, modalService, gridService, layoutConfig, validatorConfig, authService, ratingTypeService) {
		var ctrl = this, params = $scope.$stateParams.params, errorMessage = {};
		ctrl.layoutConfig = layoutConfig;
		ctrl.validatorConfig = validatorConfig;
		ctrl.ratingType = {};
		ctrl.ratingTypeOld = {};
		ctrl.init = function() {
			ratingTypeService.initData($scope, ctrl, true);
			authService.updateVerify(params).then(function(response) {
				ctrl.ratingType = response.data.model.newModel || {};
				ctrl.ratingTypeOld = response.data.model.oldModel || {};
				ctrl.ratingType.taskId = response.data.task.taskId;
				ctrl.ratingType.ratingGrades = ctrl.ratingType.ratingGrades || [];
				ctrl.ratingType.ratingQaItems = ctrl.ratingType.ratingQaItems || [];
				ctrl.ratingType.ratingQaAdjs = ctrl.ratingType.ratingQaAdjs || [];
				ctrl.ratingTypeOld.ratingGrades = ctrl.ratingTypeOld.ratingGrades || [];
				ctrl.ratingTypeOld.ratingQaItems = ctrl.ratingTypeOld.ratingQaItems || [];
				ctrl.ratingTypeOld.ratingQaAdjs = ctrl.ratingTypeOld.ratingQaAdjs || [];
				//overrid the function.
				var getOldObj = function (oldItemObjs, selectedItemObj) {
					if(angular.isArray(oldItemObjs)) {
						for(var i = 0; i < oldItemObjs.length; i++) {
							if(oldItemObjs[i].primaryKey == selectedItemObj.primaryKey){
								return oldItemObjs[i];
							}
						}
					}
				}
				ctrl.viewQaItem = function(obj) {
					ctrl.selectedRow = obj.entity;
					ctrl.oldModel4SelectedRow = getOldObj(ctrl.ratingTypeOld.ratingQaItems, ctrl.selectedRow);
					modalService.openCustomDialogs({
						'dialogsModule' : 'cda.ratingType.qaItemView',
						'params' : {
							'newModel' :ctrl.selectedRow,
							'oldModel' :ctrl.oldModel4SelectedRow
						},
						'btnCallback' : {
							'ok' : function(qaChoiceCtrl) {
								angular.forEach(ctrl.selectedQaItems, function(selectedItem, index) {
									angular.forEach(ctrl.ratingType.ratingQaItems, function(item, i) { 
										if (selectedItem.$$hashKey == item.$$hashKey) { 
											ctrl.ratingType.ratingQaItems[i].ratingQaChoices = qaChoiceCtrl.ratingQaItem.ratingQaChoices;
										}
									});
								});
							}
						}
					});
				};

				ctrl.viewQaAdj = function(obj) {
					ctrl.selectedRow = obj.entity;
					ctrl.oldModel4SelectedRow = getOldObj(ctrl.ratingTypeOld.ratingQaAdjs, ctrl.selectedRow);
					modalService.openCustomDialogs({
						'dialogsModule' : 'cda.ratingType.qaAdjView',
						'params' : ctrl.selectedQaAdjs[0],
						'params' : {
							'newModel' :ctrl.selectedRow,
							'oldModel' :ctrl.oldModel4SelectedRow
						},
						'btnCallback' : {
							'ok' : function(qaAdjdCtrl) {
								angular.forEach(ctrl.selectedQaAdjs, function(selectedItem, index) {
									angular.forEach(ctrl.ratingType.ratingQaAdjs, function(item, i) { 
										if (selectedItem.$$hashKey == item.$$hashKey) { 
											ctrl.ratingType.ratingQaAdjs[i].ratingQaAdjds = qaAdjdCtrl.ratingQaAdj.ratingQaAdjds;
										}
									});
								});
							}
						}
					});
				};
			});
		};
		
		ctrl.updateVerifyReject = function() {
			modalService.openPromptDialogs({
				'btnCallback' : {
					'ok' : function(comments) {
						ctrl.ratingType.comments = comments;
						authService.updateVerifyReject(ctrl.ratingType).then(rejectSuccessful, rejectFailed);
					}
				}
			});
		};

		ctrl.updateVerifyApprove = function() {
			modalService.openConfirmDialogs({
				'messages' : [ {msg : 'title.dialog.confirm.approve', args : [ctrl.ratingType.primaryKey]} ],
				'btnCallback' : {
					'ok' : function() {
						authService.updateVerifyApprove(ctrl.ratingType).then(approveSuccessful, approveFailed);
					}
				}
			});
		};

		function approveSuccessful(response) {
			modalService.openAcknowledgeDialogs({
				'messages' : [ 'title.dialog.successful.approve' ],
				'btnCallback' : {
					'close' : function() {
						$scope.go('cda.workflow.wfSetup.list');
					}
				},
				'redirections' : [ {
					'name' : 'title.infobar.workflow.list',
					'state' : 'cda.workflow.wfSetup.list',
					'params' : {}
				}, {
					'name' : 'title.infobar.ratingtype.list',
					'state' : 'cda.ratingType.list',
					'params' : {}
				}]
			});
		}

		function approveFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}

		function rejectSuccessful(response) {
			modalService.openAcknowledgeDialogs({
				'messages' : [ 'title.dialog.successful.reject' ],
				'btnCallback' : {
					'close' : function() {
						$scope.go('cda.workflow.wfSetup.list');
					}
				},
				'redirections' : [ {
					'name' : 'title.infobar.workflow.list',
					'state' : 'cda.workflow.wfSetup.list',
					'params' : {}
				}, {
					'name' : 'title.infobar.ratingtype.list',
					'state' : 'cda.ratingType.list',
					'params' : {}
				}]
			});
		}

		function rejectFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}
	}
})(angular);